package com.bana.fvs.app.module.tab2.tab2Detail.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.module.tab2.Tab2CommonFragment;
import com.bana.fvs.app.module.tab2.tab2Detail.entity.EquipListItemBean;

import java.util.List;

/**
 * Author : yanftch
 * Date : 2018/2/27
 * Time : 21:29
 * Desc :
 */

public class Tab2CommonInnerAdapter extends RecyclerView.Adapter<Tab2CommonInnerAdapter.ViewHolder> {
    private static final String TAG = "dah_Tab2CommonInnerAdapter";
    private List<EquipListItemBean> datas;
    private int type;
    private String sbzt="";

    public Tab2CommonInnerAdapter(List<EquipListItemBean> datas, int type) {
        this.type = type;
        this.datas = datas;
    }

    public void setSbzt(String sbzt) {
        this.sbzt = sbzt;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout_equipment_view, parent, false);
        return new Tab2CommonInnerAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(Tab2CommonInnerAdapter.ViewHolder holder, int position) {
        if (type == 0) {
        } else if (type == 1) {
        } else if (type == 2) {
        }
        EquipListItemBean bean = datas.get(position);
        // TODO: 2018/2/27 根据接口返回的字段，判断状态，显示不同的图片资源(本地图片)
        if (Tab2CommonFragment.Params.request_param_sbzt_1.equals(bean.getSbzt())) {//正常
           if("2".equals(sbzt)){
               holder.setVisibility(false);
               //holder.itemBaseContainer.setVisibility(View.GONE);
           }else{
               holder.setVisibility(true);
               //holder.itemBaseContainer.setVisibility(View.VISIBLE);
           }
            holder.errorEquipState.setImageResource(R.drawable.equipment_normal);
        } else if (Tab2CommonFragment.Params.request_param_sbzt_2.equals(bean.getSbzt())) {//异常
            if("1".equals(sbzt)){
                holder.setVisibility(false);
                //holder.itemBaseContainer.setVisibility(View.GONE);
            }else{
                holder.setVisibility(true);
               // holder.itemBaseContainer.setVisibility(View.VISIBLE);
            }

            holder.errorEquipState.setImageResource(R.drawable.equipment_error);
        }
        holder.errorTvName.setText(bean.getName());
        holder.tvFogName.setVisibility(View.VISIBLE);
        holder.tvFogName.setText(bean.getWqmc());
        holder.itemBaseContainer.setOnClickListener(v -> {
            if (null != mOnItemClickListener) {
                mOnItemClickListener.onClickListener(position);
            }
        });
        //
        ImageView imageView = holder.errorRightHandleIcon;
        imageView.setOnClickListener(v -> {
            if (null != mListener) {
                mListener.onOnHandleBtnClick(position, imageView);
            }
        });
    }

    @Override
    public int getItemCount() {
        return null == datas ? 0 : datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        //最外层容器
        LinearLayout itemBaseContainer;
        //Error页面
        private RelativeLayout itemSingleErrorContainer;
        private TextView errorTvName, tvFogName;
        private ImageView errorEquipState;
        private ImageView errorRightHandleIcon;


        public ViewHolder(View itemView) {
            super(itemView);
            itemBaseContainer = itemView.findViewById(R.id.itemBaseContainer);

            /**-----------------------------Error------------------------------------*/
            itemSingleErrorContainer = itemView.findViewById(R.id.itemSingleErrorContainer);
            errorTvName = itemView.findViewById(R.id.errorTvName);
            errorEquipState = itemView.findViewById(R.id.errorEquipState);
            errorRightHandleIcon = itemView.findViewById(R.id.errorRightHandleIcon);
            tvFogName = itemView.findViewById(R.id.tv_fog_name);
            /**---------------------------------------------------------------------*/

        }

        public void setVisibility(boolean isVisible){
            RecyclerView.LayoutParams param = (RecyclerView.LayoutParams)itemView.getLayoutParams();
            if (isVisible){
                param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                param.width = LinearLayout.LayoutParams.MATCH_PARENT;
                itemView.setVisibility(View.VISIBLE);
            }else{
                itemView.setVisibility(View.GONE);
                param.height = 0;
                param.width = 0;
            }
            itemView.setLayoutParams(param);
        }
    }

    /**
     * 回调
     */
    private SingleAllAdapter.onItemClickListener mOnItemClickListener;

    public interface onItemClickListener {
        void onClickListener(int position);
    }

    public void setOnItemClickListener(SingleAllAdapter.onItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    /**
     * 右边图片点击回调
     */
    private SingleAllAdapter.onRightHandleIconClickListener mListener;

    public interface onRightHandleIconClickListener {
        void onOnHandleBtnClick(int position, View view);
    }

    public void setOnHandleBtnClickListener(SingleAllAdapter.onRightHandleIconClickListener l) {
        this.mListener = l;
    }
}
